package com.twotiger.and.activity.others;

import java.util.HashMap;

import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.activity.user.LoginPage;
import com.twotiger.and.activity.user.SetGesturePsdPage;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.User;
import com.twotiger.and.util.Tools;

/**
 * 确认密码
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月15日 下午10:33:53
 */
public class EnsurePassword extends BaseActivity {

    private static final int LOGINSUCCES = 0;
    private static final int LOGINFAIL = 1;
    private CheckBox new_pwd_plaintext;
    private TextView forget_pwd;
    private Button next_btn;
    private EditText input_pwd;

    private HashMap<String, String> parm;

    private SubTitle subTitle;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.ensure_pwd_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        new_pwd_plaintext = (CheckBox) view.findViewById(R.id.new_pwd_plaintext);
        forget_pwd = (TextView) view.findViewById(R.id.forget_pwd);
        next_btn = (Button) view.findViewById(R.id.next_btn);
        input_pwd = (EditText) view.findViewById(R.id.input_pwd);
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("验证密码");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
    }

    @Override
    protected void processLogic() {
        parm = getParm();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if(!isLogin()){
           finish(); 
        }
    }

    @Override
    protected void setListener() {
        next_btn.setOnClickListener(this);
        forget_pwd.setOnClickListener(this);
        new_pwd_plaintext.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    input_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    input_pwd.setInputType(InputType.TYPE_CLASS_TEXT
                                           | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                Editable etable = input_pwd.getText();
                Selection.setSelection(etable, etable.length());
            }
        });
        input_pwd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() < 8 ) {
                    next_btn.setEnabled(false);
                } else {
                    next_btn.setEnabled(true);
                }
            }
        });
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case LOGINSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        User user = JSONObject.parseObject(result.data, User.class);
                        Intent intent = new Intent(getContext(), SetGesturePsdPage.class);
                        startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
                    } else {
                        showToast(result.codeDesc);
                    }
                    break;
                case LOGINFAIL:

                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.next_btn:
            String psw = input_pwd.getText().toString().trim();
            if (TextUtils.isEmpty(psw)) {
                return;
            }
            login(psw);
            break;
        case R.id.forget_pwd:
//            Intent intent = new Intent(mContext, FindPwdPage.class);
//            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            Intent intent = new Intent(mContext, LoginPage.class);
            intent.putExtra("witchLayout", "findpwd");
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            break;
        default:
            break;
        }

    }

    private void login(String psw) {
        parm.clear();
        parm.put("phone", application.getUser().phone);
        parm.put("password", psw);
        parm.put("deviceId", Tools.getDeviceId(mContext));
        getDate(parm, Constants.LOGIN, handler, LOGINSUCCES, LOGINFAIL, true, true, false);
    }

}
